package client.comm.baoding.ui.vm

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.Util
import client.comm.baoding.ui.viewmodel.MiddleVm


class XgzjmmViewModel(app: Application) : MiddleVm(app) {

    val forgetRet = MutableLiveData<JsonResult<Ret>>()
    val codeRet = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>()


    val password = MutableLiveData<String>()
    val smscode = MutableLiveData<String>()
    val phone = MutableLiveData<String>()
    val username = MutableLiveData<String>()
    val getCodeTxt = MutableLiveData<String>("获取验证码")
    val getCodeEnable = MutableLiveData<Boolean>(true)
    val passwordAgain = MutableLiveData<String>()

    val isShowPwd = MutableLiveData<Boolean>(false)
    val isShowPwdAgain = MutableLiveData<Boolean>(false)


    fun switchIsShowPwd() {
        isShowPwd.value = !isShowPwd.value!!
    }

    fun switchIsShowPwdAgain() {
        isShowPwdAgain.value = !isShowPwdAgain.value!!
    }

    /**
     *
     */
    fun ok() {

        if (TextUtils.isEmpty(phone.value) || TextUtils.isEmpty(smscode.value) || TextUtils.isEmpty(
                password.value
            ) || TextUtils.isEmpty(
                passwordAgain.value
            )
        ) {
            toast("所有不能为空")
            return
        }

        if (!password.value.equals(passwordAgain.value)) {
            toast("二次输入密码不一致")
            return
        }

//        repository.xgzjpwd(passwordOld.value!!, password.value!!, forgetRet, isShowing)
        hold({
            server.forget_pay(
                username.value!!,
                phone.value!!,
                smscode.value!!,
                password.value!!,
                passwordAgain.value!!
            )
        }, {
            isShowing.value = true

            codeRet.value = null
        }, {
            forgetRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun getCode() {

        if (TextUtils.isEmpty(phone.value)) {
            toast("手机号不能为空")
            return
        }
        hold({
            val ts = System.currentTimeMillis()
            server.getcode(
                phone.value!!,
                "${ts}", Util.getCaptchaSign(phone.value!!, "${ts}")
            )
        }, {
            isShowing.value = true
            getCodeEnable.value = false
            codeRet.value = null
        }, {
            codeRet.value = it
        }, complete = {
            isShowing.value = false
        }, err = {
            getCodeEnable.value = true
        })

    }

}